//Variable globale des mois
var mmToMonth = new Array("Janv", "Fév", "Mar", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Dec");

function surchargeSelectRoomDevicePerson(){
    var eventDialog = document.getElementById("sun-calendar-event-dialog");
    eventDialog.setAttribute("onload","onLoad();getListSallesAddEvent();getListDevicesAddEvent();getListPersonnesAddEvent(); getListInvitesAddEvent();");
}

function surchargeBookRoomDevicePerson(){
    var eventSave = document.getElementById("cmd_accept");
    eventSave.setAttribute("oncommand","acceptDialog();bookARoomDevicePerson(idRoom, idPerson);");
}

/*
 * MPA : Fonction permettant la r�servation d'une salle, du mat�riel et de la personne � un �v�nement
 */
function bookARoomDevicePerson(idRoom, idPerson){
	
	//R�cup�ration des dates, heures, salle
    var evtStartTime = document.getElementById('event-starttime').value;
    var evtEndTime = document.getElementById("event-endtime").value;
    
    //Avant formatage
    dump("StartTime = "+evtStartTime+"\n");
    dump("EndTime = "+evtEndTime+"\n");
    
    var tsStartTime = new Date(evtStartTime);
    var tsEndTime = new Date(evtEndTime);
    
    //Variables d'�v�nement de d�but
    var roomBooked = idRoom;

    //dump("StartTimeTS = "+tsStartTime+"\n");
    //dump("EndTimeTS = "+tsEndTime+"\n");
    
    
    //Requ�te de r�cup�ration de l'ID de l'event qui vient d'�tre cr��
    var reqIdEvent = "SELECT * FROM cal_events where rowid = (select max(rowid) from cal_events)";	
    var statement = executeReqSqlOrig(reqIdEvent);
    while(statement.step()){
	    var idEvent = statement.row.id;
    }
    //dump("idEvent = "+idEvent);
	
    //recuperation des matériels sélectionnés
	var listDevice = document.getElementById("event-grid-select-device-box");
	var count = listDevice.selectedCount;
	while (count--){
	    var idDevice = listDevice.selectedItems[count].value;
	    //recuperation des invités sélectionnés
	    var listPerson = document.getElementById("event-grid-select-person-invite-box");
		var countPerson = listPerson.selectedCount;
		while (countPerson--){
		    var idInvite = listPerson.selectedItems[countPerson].value;
	    
		    //Requ�te d'ajout de la r�servation
		    var requeteSql = "INSERT INTO reservation ('dateReservation','dateReturn','idRoom','idEvent', 'idDevice', 'idReservant', 'idPerson') VALUES (:dateReservation,:dateReturn,:idRoom,:idEvent,:idDevice,:idReservant,:idPerson)";
		
		    //Ex�cution de la requete et r�cup�ration des donn�es
		    var arraySql = insertReqSql(requeteSql);
		    var db = arraySql[0];
		    var statement = arraySql[1];
		
		    statement.initialize(db.createStatement(requeteSql));
		    
		    //statement.params.dateReservation = evtStartTime;
		    //statement.params.dateReturn = evtEndTime;
		    statement.params.dateReservation = tsStartTime;
		    statement.params.dateReturn = tsEndTime;
		    
		    statement.params.idRoom = roomBooked;
		    statement.params.idEvent = idEvent;
		    statement.params.idDevice = idDevice;
		    statement.params.idReservant = idPerson;
		    statement.params.idPerson = idInvite;
		    
		    statement.step();
		    
		    dump("Room booked!");
		}
	}
}


/*
 * MPA : Fonction de d�coupe des dates
 * Nous pouvons passer d'un timestamp stock� en base � une date normale
 */
function showLocalDate(timestamp)
{
  var dt = new Date(timestamp*1000);
  var mm = mmToMonth[dt.getMonth()];
  return dt.getDate()+" "+mm+" "+dt.getFullYear()+" "+dt.getHours()+"h"+dt.getMinutes();
} 